package org.kl.bf.service.basic;

import java.util.List;

import org.kl.bf.entity.basic.Config;
import org.kl.bf.repository.basic.ConfigDao;
import org.kl.bf.service.BasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.utils.Collections3;

import com.alibaba.fastjson.JSON;

/**
 * @Author hqm
 * @CreateTime 2014-3-13 下午9:11:30
 */
// Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional
public class ConfigService extends BasicService<Config> {
	@Autowired
	private ConfigDao configDao;

	// 清缓存
	public void flushConfig() {
		configDao.flush();
	}

	public void save(Config entity, @RequestParam(value = "inserted") String detailInserted,
			@RequestParam(value = "updated") String detailUpdated, @RequestParam(value = "deleted") String detailDeleted) {
		basicDao.save(entity);
		List<Config> inserts = JSON.parseArray(detailInserted, Config.class);
		if (Collections3.isNotEmpty(inserts)) {
			for (Config a : inserts) {
				// 增加与主表相关的数据复制代码
				configDao.save(a);
			}
		}
		List<Config> updates = JSON.parseArray(detailUpdated, Config.class);
		if (Collections3.isNotEmpty(updates)) {
			for (Config b : updates) {
				configDao.save(b);
			}
		}
		List<Config> deletes = JSON.parseArray(detailDeleted, Config.class);
		if (Collections3.isNotEmpty(deletes)) {
			configDao.delete(deletes);
		}
	}
}
